using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace FarmDemo.Controllers;


[ApiController]
[Route("[controller]/[action]")]
public class ServiceController : ControllerBase
{

    private readonly HttpClient _client;
    public ServiceController(HttpClient client)
    {
        _client = client;
    }


    [HttpPost]
    public async Task<IActionResult> PostImage([FromBody] byte[] image)
    {
        // 将图像传递给算法服务器的地址
        var url = "https://example.com/algorithm";
        // 创建一个 HttpContent 对象，包含图像数据
        var content = new ByteArrayContent(image);
        // 发送 POST 请求，并获取响应
        var response = await _client.PostAsync(url, content);
        // 判断响应是否成功
        if (response.IsSuccessStatusCode)
        {
            // 读取响应中的处理结果
            var result = await response.Content.ReadAsStringAsync();
            // 返回 200 状态码和处理结果
            return Ok(result);
        }
        else
        {
            // 返回错误状态码和消息
            return StatusCode((int)response.StatusCode, response.ReasonPhrase);
        }
    }

}


